import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { MenuHandle } from "src/modules/system/menu/entity/handle.entity";
import { MenuGtag } from "src/modules/system/menu/entity/menuGtag.entity";
import { Repository } from "typeorm";

@Injectable()
export class AuthRepositry {

  constructor(
    @InjectRepository(MenuHandle) private readonly menuGtag: Repository<MenuHandle>
  ) { }

  /**
    * 查询当前角色是否有操作权限
    * @param handleCode 操作按钮code
    * @param roleIds 角色Ids
    * @returns true or false
    */
  async getHandlePermission(handleCode: string, roleIds: number[]): Promise<boolean> {
    const result = await this.menuGtag
      .createQueryBuilder('handle')
      .innerJoin(
        MenuGtag, 'gtag', 'handle.id = gtag.handleId'
      )
      .where("handle.delete_flag = false")
      .andWhere(`handle.code = '${handleCode}'`)
      .andWhere(`gtag.roleId in (${roleIds})`)
      .getMany()

    return result.length > 0
  }
}